package com.xcw.service.impl;

import com.xcw.entity.req.UserLoginParams;
import com.xcw.entity.model.User;
import com.xcw.mapper.UserMapper;
import com.xcw.service.LoginService;
import com.xcw.util.RedisUtils;
import com.xcw.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;


/**
 * @Author bhx
 * @Date 2022/6/22 22:50
 * @Description TODO
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    private UserMapper userMapper;

    @Override
    public Result loginAuthentication(UserLoginParams userLoginParams) {

        Result result = new Result();
        List<User> sysUsers = userMapper.getUserList(userLoginParams);
        Assert.notEmpty(sysUsers,"用户名不存在，请检查用户名信息！！");
        if (userLoginParams.getPassword().equals(sysUsers.get(0).getPwd())) {
            String accessToken = "accessToken:" + UUID.randomUUID();
            redisUtils.set(accessToken,"xxxx", 1800);
            System.out.println(accessToken);
            result.put("200", "用户登录成功");
        } else {
            result.put("301", "密码不正确");
        }
        return result;
    }
}
